package com.enjoyor.soa.traffic.frame.support.util;

import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.PageEasyUI;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class CommonUtil {
    public CommonUtil() {
    }

    @SuppressWarnings("rawtypes")
    public static PageEasyUI getPage(HttpServletRequest request) {
        PageEasyUI page = new PageEasyUI();
        String currentPage = request.getParameter("currentPage") == null ? "" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "" : request.getParameter("pageSize");
        if (currentPage.compareTo("") == 0) {
            page.setCurrentPage(1);
        } else {
            page.setCurrentPage(Integer.valueOf(currentPage));
        }

        if (pageSize.compareTo("") == 0) {
            page.setDefaultPageSize();
        } else {
            page.setPageSize(Integer.valueOf(pageSize));
        }

        return page;
    }

    @SuppressWarnings("rawtypes")
    public static Page getPageInfo(HttpServletRequest request) {
        Page page = new Page();
        String currentPage = request.getParameter("currentPage") == null ? "" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "" : request.getParameter("pageSize");
        if (currentPage.compareTo("") == 0) {
            page.setCurrentPage(1);
        } else {
            page.setCurrentPage(Integer.valueOf(currentPage));
        }

        if (pageSize.compareTo("") == 0) {
            page.setDefaultPageSize();
        } else {
            page.setPageSize(Integer.valueOf(pageSize));
        }

        return page;
    }

    @SuppressWarnings("rawtypes")
    public static Page getPageInfo(String data) {
        Page page = new Page();

        try {
            JSONObject jsonpara = JSONObject.parseObject(data);
            if (jsonpara != null) {
                String currentPage = jsonpara.getString("currentPage") != null ? jsonpara.getString("currentPage") : "";
                String pageSize = jsonpara.getString("pageSize") != null ? jsonpara.getString("pageSize") : "";
                String totalCount = jsonpara.getString("totalCount") != null ? jsonpara.getString("totalCount") : "";
                String sortExp = jsonpara.getString("sortExp") != null ? jsonpara.getString("sortExp") : "";
                String sortDir = jsonpara.getString("sortDir") != null ? jsonpara.getString("sortDir") : "";
                String currentCount = jsonpara.getString("currentCount") != null ? jsonpara.getString("currentCount")
                        : "";
                if (currentPage.compareTo("") == 0) {
                    page.setCurrentPage(1);
                } else {
                    page.setCurrentPage(Integer.valueOf(currentPage));
                }

                if (pageSize.compareTo("") == 0) {
                    page.setDefaultPageSize();
                } else {
                    page.setPageSize(Integer.valueOf(pageSize));
                }

                if (totalCount.compareTo("") != 0) {
                    page.setTotalCount(Integer.valueOf(totalCount));
                }

                if (sortDir.compareTo("") == 0) {
                    page.setSortDir(sortDir);
                }

                if (sortExp.compareTo("") == 0) {
                    page.setSortExp(sortExp);
                }

                if (currentCount.compareTo("") != 0) {
                    page.setCurrentResult(Integer.valueOf(currentCount));
                }
            }
        } catch (Exception var9) {
            LoggerHelper.LOG.error(var9.getMessage());
        }

        return page;
    }

    @SuppressWarnings("rawtypes")
    public static PageEasyUI getPage(String data) {
        PageEasyUI page = new PageEasyUI();

        try {
            JSONObject jsonpara = JSONObject.parseObject(data);
            if (jsonpara != null) {
                String currentPage = jsonpara.getString("currentPage") != null ? jsonpara.getString("currentPage") : "";
                String pageSize = jsonpara.getString("pageSize") != null ? jsonpara.getString("pageSize") : "";
                String sortExp = jsonpara.getString("sortExp") != null ? jsonpara.getString("sortExp") : "";
                if (currentPage.compareTo("") == 0) {
                    page.setCurrentPage(1);
                } else {
                    page.setCurrentPage(Integer.valueOf(currentPage));
                }

                if (pageSize.compareTo("") == 0) {
                    page.setDefaultPageSize();
                } else {
                    page.setPageSize(Integer.valueOf(pageSize));
                }

                if (sortExp.compareTo("") == 0) {
                    page.setSortExp(sortExp);
                }
            }
        } catch (Exception var6) {
            LoggerHelper.LOG.error(var6.getMessage());
        }

        return page;
    }

    @SuppressWarnings("rawtypes")
    public static Page getPageEasyui(HttpServletRequest request) {
        Page page = new Page();
        String currentPage = request.getParameter("page") == null ? "" : request.getParameter("page");
        String pageSize = request.getParameter("rows") == null ? "" : request.getParameter("rows");
        if (currentPage.compareTo("") == 0) {
            page.setCurrentPage(1);
        } else {
            page.setCurrentPage(Integer.valueOf(currentPage));
        }

        if (pageSize.compareTo("") == 0) {
            page.setDefaultPageSize();
        } else {
            page.setPageSize(Integer.valueOf(pageSize));
        }

        return page;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Page getPageInfoByList(Page page, List list) {
        try {
            int totalCount = list.size();
            page.setTotalCount(totalCount);
            int pageSize = page.getPageSize();
            int totalPage = totalCount / pageSize + 1;
            int currentPage = page.getCurrentPage();
            page.setTotalPage(totalPage);
            if (currentPage != 0) {
                List newList = new ArrayList();
                int a = currentPage * pageSize;
                int b = a > totalCount ? totalCount : a;

                for (int i = (currentPage - 1) * pageSize; i < b; ++i) {
                    newList.add(list.get(i));
                }

                page.setResult(newList);
            } else {
                page.setResult(list);
            }
        } catch (Exception var10) {
            LoggerHelper.LOG.error("getPageInfoByList调用异常！", var10);
        }

        return page;
    }
}
